package com.example.haojiutao.service.impl;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.mapper.CollectMapper;
import com.example.haojiutao.model.domain.Collect;
import com.example.haojiutao.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:收藏功能
 * @Author: ma_lihong
 * @CreateTime： 2024/1/31 16:53
 */
@Slf4j
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;

    /*
     * @Description: 根据,userid查看收藏该商品
     * @Author: ma_lihong
     * @CreateTime: 2024/2/1 16:27
     * @param: userId
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public List<Collect> findByUserId(Integer userId) {
        return collectMapper.findByUserId(userId);

    }



    /*
     * @Description: 根据goods_id 删除收藏记录
     * @Author: ma_lihong
     * @CreateTime: 2024/2/1 16:27
     * @param: id
     * @return: int
     **/
    @Override
    public int deleteCollect(int id) {
        return collectMapper.deleteCollect(id);
    }



    /*
     * @Description: 收藏商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/31 17:00
     * @param: collect
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public Result addCollect(Collect collect) {
 //       int goodsId=collect.getGoodsId();
//        int sellerId=collect.getSellerId();
//        String goodsName=collect.getGoodsName();
//       int collects= collectMapper.isCollect(goodsId).getCode();
//
//        if(collects >0){
//            throw new TreasureBoxException("100005","商品已存在，请更改");
//        }
//        log.info("添加成功");
        collect.setCollectTime(new Date());

        int row=collectMapper.addCollect(collect);

        if(row==1){
            return Result.success("收藏成功");
        }
        return Result.faile("收藏失败");
    }

    /*
     * @Description: 收藏商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/31 17:00
     * @param: collect
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public Collect findCollect(int userId, int goodsId) {
        Collect collectinfo=collectMapper.findCollect(userId,goodsId);
        return collectinfo;
    }

    /*
     * @Description: 根据,goodsId查看收藏该商品的信息
     * @Author: ma_lihong
     * @CreateTime: 2024/2/1 16:29
     * @param: goodsId
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public List<Collect> getByGoodsId(int goodsId) {
        List<Collect> list =collectMapper.getByGoodsId(goodsId);

        return list;
    }

    /*
     * @Description: 获取收藏数
     * @Author: ma_lihong
     * @CreateTime: 2024/2/2 16:29
     * @param: userId
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public Integer getCountByGoodsId(int goodsId) {
        Integer collectCount =collectMapper.getCountByGoodsId(goodsId);
        return collectCount;
    }

    /*
     * @Description: 收藏更新
     * @Author: ma_lihong
     * @CreateTime: 2024/2/2 16:29
     * @param: collect
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public void updateCollectStatus(Integer id) {
        collectMapper.updateCollectStatus(id);

    }


    /*
     * @Description: 查询用户所有收藏
     * @Author: ma_lihong
     * @CreateTime: 2024/2/2 16:29
     * @param: collect
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public List<Collect> selectCollect(Collect collect) {
        return collectMapper.selectCollect(collect);
    }
}
